using System.Threading.Tasks;
using Admin.NET.Application.AttributeInfo;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.NET.Application;

/// <summary>
/// 电价方案服务
/// </summary>
[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class PriceRuleService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<ElectricityPriceRule> _rep;

	public PriceRuleService(SqlSugarRepository<ElectricityPriceRule> rep)
	{
		_rep = rep;
	}

	/// <summary>
	/// 分页查询电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/priceRule/page")]
	public async Task<dynamic> Page([FromQuery] PriceRuleInput input)
	{
		ISugarQueryable<ElectricityPriceRule> query = _rep.Context.Queryable<ElectricityPriceRule>().WhereIF(!string.IsNullOrWhiteSpace(input.Name), (ElectricityPriceRule u) => u.Name.Contains(input.Name.Trim())).WhereIF(!string.IsNullOrWhiteSpace(input.Description), (ElectricityPriceRule u) => u.Description.Contains(input.Description.Trim()));
		query = query.OrderBuilder(input);
		return await query.ToPagedListAsync(input.Page, input.PageSize);
	}

	/// <summary>
	/// 增加电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/priceRule/add")]
	public async Task Add(AddPriceRuleInput input)
	{
		ElectricityPriceRule entity = input.Adapt<ElectricityPriceRule>();
		await _rep.InsertAsync(entity);
	}

	/// <summary>
	/// 删除电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/priceRule/delete")]
	public async Task Delete(DeletePriceRuleInput input)
	{
		ElectricityPriceRule entity = await _rep.GetFirstAsync((ElectricityPriceRule u) => u.Id == input.Id);
		await _rep.DeleteAsync(entity);
	}

	/// <summary>
	/// 更新电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/priceRule/edit")]
	[OperationLog("更新电价方案", "name")]
	public async Task Update(UpdatePriceRuleInput input)
	{
		ElectricityPriceRule entity = input.Adapt<ElectricityPriceRule>();
		if (entity.Type == 0)
		{
			entity.DeepValleyPrice = null;
			entity.FlatPrice = null;
			entity.PeakPrice = null;
			entity.SharpPrice = null;
			entity.ValleyPrice = null;
		}
		else
		{
			entity.Price = null;
		}
		await _rep.UpdateAsync(entity);
	}

	/// <summary>
	/// 获取电价方案
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/priceRule/detail")]
	public async Task<ElectricityPriceRule> Get([FromQuery] QueryePriceRuleInput input)
	{
		return await _rep.GetFirstAsync((ElectricityPriceRule u) => u.Id == input.Id);
	}

	/// <summary>
	/// 获取采集器2列表
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/priceRule/list")]
	public async Task<dynamic> List([FromQuery] PriceRuleInput input)
	{
		return await _rep.AsQueryable().ToListAsync();
	}
}
